__usage__='''call examples:
python run_crosswind.py -quite -problem=problemName -p=degree -N=res ... supress colors and progress information.
python run_crosswind.py -problem=problemName -p=degree -N=res ... colors and progress information.

problemName can be'rs' for rotated square,'ds' for driven square, 'zd' for Zalesak's disk, 'all' for all the prob-
lems.
degree is int, polynomial degreee of approx. base functions.
res is int, spatial resolution for 'ds' domain has [2*N,N] squares, 'ds' and 'zs' have [2*N,2*N]
'''

from problems.aux import get_problem
from solvers.codina import *
from graphics.lineplot import LinePlot
from graphics.colorprint import ColorPrint,colors
import time,sys,random

if __name__ == '__main__':
    if not(len(sys.argv) == 4 or len(sys.argv) == 5):
        print __usage__
        sys.exit()
    elif len(sys.argv) == 4:
        if not('-problem=' in sys.argv[1][:9] and '-p=' in sys.argv[2][:3] and '-N=' in sys.argv[3][:3]):
            print __usage__
            sys.exit()
        else:
            sColor = 'red'
            iColor = random.choice(colors.keys())
            pgb = True
            problemName = sys.argv[1][9:]
            p = int(sys.argv[2][3:])
            N = int(sys.argv[3][3:])
    else: 
        if not(sys.argv[1] == '-quite' and '-problem=' in sys.argv[2][:9] and '-p=' in sys.argv[3][:3] and '-N=' in sys.argv[4][:3]):
            print __usage__
            sys.exit()
        else:
            sColor = 'normal'
            iColor = 'normal'
            pgb = False
            problemName = sys.argv[2][9:]
            p = int(sys.argv[3][3:])
            N = int(sys.argv[4][3:])

    options={}
    options ['N'] = N           # spatial resolution
    options['p'] = p            # polynomial degree
    options['results-path'] = './results/results-stabilized-N=%d-p=%d' % (options['N'],options['p']) # path to results 

    lp = LinePlot()
    cp = ColorPrint(colors)

    be = backwardeuler.BackwardEuler(options['p'])  
    cn = cranknicolson.CrankNicolson(options['p'])
    g4 = gausslegendre4.GaussLegendre4(options['p'])

    for solver in [be,cn,g4]:
        solver.pgb = pgb
        for problem, sf in get_problem(problemName,options):
            solver.sf = sf
            
            print '%s' % ('-'*70)
            print cp('Solving %s problem using %s %s method:' % (problem.problemDir,solver.methodDir,solver.solverDir),iColor)
            start = time.time()
            solver.solve(problem)
            lp.plot_all(problem,solver)
            print cp('\nDone(CFL=%g) in %s%s' % (solver.CFL,cp('%g s' % (time.time()-start),sColor),cp('!',iColor)),iColor)
            print '%s' % ('-'*70)
            
            del problem
        del solver

